package com.hhj.dao.impl;

import com.hhj.dao.BaseDao;
import com.hhj.dao.UserDao;
import com.hhj.pojo.User;
import com.hhj.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl extends BaseDao implements UserDao{
    @Override
    public User queryUserByID(int id) {
        String sql = "select * from user where id = ?";
        Object[] arr = {id};
        Connection con = JDBCUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = query(sql, arr, con, ps);
        User user = new User();
        try {
            while (rs.next()){
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //释放资源
            close(con,ps,rs);
        }

        return user;
    }

    @Override
    public int addUser(User user) {
        String sql ="insert into user(id,name)values(?,?)";
        Object[] arr = {user.getId(),user.getName()};
        int cud = cud(sql, arr);
        return cud;
    }
}
